import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.table.TableModel;

import se.datadosen.component.ControlPanel;
import se.datadosen.component.RiverLayout;

public class Gui {
	private Object[][] ingExample = { { "Flour", "300", "30000" },
			{ "Butter", "500", "56000" }, { "Sugar", "200", "23000" } };
	private Object[][] prodExample = { // vi m�ste anv�nda TableModel f�r att check box
								// ska visas i JTable
	{ "Nut Ring", "2", new Boolean(false) },
			{ "Nut Cookie", "15", new Boolean(false) }, };
	private String[] cookiesNames = { "Nut Ring", "Nut Cookie" };
	private JLabel[] ingNames = new JLabel[6];
	private JLabel[] reqAm = new JLabel[6];
	private JLabel[] avaAm = new JLabel[6];
	private static final int TABLE_WIDTH = 600;
		
	public Gui() {
		
		// Create and set up the window.
		JFrame frame = new JFrame(
				"Krusty Kookies AB - Production Department Software");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ControlPanel mainPanel = new ControlPanel();
		mainPanel.setLayout(new GridLayout(1, 1));
		mainPanel.setPreferredSize(new Dimension(100, 100));
		// Menu
		// TODO
		// Tabs
		JTabbedPane tabbedPane = new JTabbedPane();
		JComponent productionTab = makeProductionTab(new Model());
		tabbedPane.addTab("Production", productionTab);
		tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

		JComponent searchTab = makeSearchTab(new Model());
		tabbedPane.addTab("Search", searchTab);
		tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
		// The following line enables to use scrolling tabs.
		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

		frame.add(tabbedPane);
		// Display the window.
		frame.pack();
		frame.setVisible(true);

	}

	private JComponent makeProductionTab(TableModel tableModel) {
		ControlPanel pTab = new ControlPanel();
		pTab.add("hfill", makeInputPanel());
		pTab.add("br hfill", makeIngrListPanel());
		pTab.add("br hfill", makeProdPanel());
		return pTab;
	}

	private ControlPanel makeIngrListPanel() {
		ControlPanel ingrListPanel = new ControlPanel();
		ingrListPanel.setTitle("Recipe");
		
		for(int i=0;i<ingNames.length;i++){
			ingNames[i]= new JLabel("");
		}
		for(int i=0;i<reqAm.length;i++){
			reqAm[i]= new JLabel("");
		}
		for(int i=0;i<avaAm.length;i++){
			avaAm[i]= new JLabel("");
		}		
		JLabel header1 = new JLabel("Ingredient name");
		JLabel header2 = new JLabel("Required(g/100pcs)");
		JLabel header3 = new JLabel("Available(grams)");
		ingrListPanel.add("",header1);
		ingrListPanel.add("tab",header2);
		ingrListPanel.add("tab",header3);
		for(int i=0; i<6;i++){
			ingrListPanel.add("br",ingNames[i]);
			ingrListPanel.add("tab",reqAm[i]);
			ingrListPanel.add("tab",avaAm[i]);
		}

		return ingrListPanel;
	}

	private ControlPanel makeProdPanel() {
		ControlPanel prodPanel = new ControlPanel();
		prodPanel.setTitle("Your production order");
		String[] columnNames = { "Product name", "# of pallets", "" };
		JTable prodTable = new JTable(prodExample, columnNames);
		JScrollPane prodTablePane = new JScrollPane(prodTable);
		prodTablePane.setPreferredSize(new Dimension(TABLE_WIDTH, 300));
		JButton remove = new JButton("Remove");
		JButton produce = new JButton("Produce");

		prodPanel.add("hfill", prodTablePane);
		prodPanel.add("br  right", remove);
		prodPanel.add("right", produce);

		return prodPanel;
	}

	private ControlPanel makeInputPanel() {
		ControlPanel inputPanel = new ControlPanel();
		inputPanel.setTitle("Input");

		JLabel prod = new JLabel("Choose product: ");
		JLabel amou = new JLabel("Decide amount(pallet)");
		final JComboBox productList = new JComboBox(cookiesNames);
		productList.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent event) {
				if (event.getStateChange() == ItemEvent.SELECTED) {
					ctrl.fetchIngredients((String)productList.getSelectedItem());
					
					updateIngredientList(ingredients);
				}
			}
		});
		JTextField amountField = new JTextField();
		JButton addToProd = new JButton("Add to production order");
		inputPanel.add("left", prod);
		inputPanel.add("tab hfill", productList);
		inputPanel.add("tab", amou);
		inputPanel.add("tab hfill", amountField);
		inputPanel.add("br right", addToProd);

		return inputPanel;
	}

	private JComponent makeSearchTab(TableModel tableModel) {
		Object[][] data = {
				{ "Kathy", "Smith", "Snowboarding", new Integer(5),
						new Boolean(false) },
				{ "John", "Doe", "Rowing", new Integer(3), new Boolean(true) },
				{ "Sue", "Black", "Knitting", new Integer(2),
						new Boolean(false) },
				{ "Jane", "White", "Speed reading", new Integer(20),
						new Boolean(true) },
		// {"Joe", "Brown",
		// "Pool", new Integer(10), new Boolean(false)}
		};
		String[] columnNames = { "First Name", "Last Name", "Sport",
				"# of Years", "Vegetarian" };
		JPanel panel = new JPanel(false);
		panel.setLayout(new GridLayout(1, 1));
		JTable table = new JTable(data, columnNames);
		JScrollPane tableContainer = new JScrollPane(table);
		panel.add(tableContainer, BorderLayout.CENTER);
		return panel;
	}
	
	public void updateIngredientList(String[] ings, String[] req, String[] ava){
		for(int i=0;i<reqAm.length;i++){
			reqAm[i].setText("");
			ingNames[i].setText("");
			avaAm[i].setText("");
		}
		//update ing names
		for(int i=0;i<ings.length;i++){
			ingNames[i].setText(ings[i]);
		}
		//update req amount

		for(int i=0;i<req.length;i++){
			reqAm[i].setText(req[i]);
		}
		//update ava amount
		for(int i=0;i<ava.length;i++){
			avaAm[i].setText(req[i]);
		}
	}
}
